package com.lk.api.app;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

import com.lk.api.entity.Users;

/**
 * Created by lk on 2014/12/24.
 */
public class DBOperation {
    //  创建表sql语句
    String sqlite = "create TABLE users(_id INTEGER PRIMARY KEY AUTOINCREMENT,names VARCHAR(50),pwd VARCHAR(50));";
    //  上下文
    private Context mContext;
    //  数据库表名
    String TABLE_DATABASE_NAME = "databaseName";

    private SQLiteDatabase mSqLiteDatabase;

    public DBOperation(Context context) {
        mContext = context;
    }

    public void openOrCreateDatabase() {
        //  mSqLiteDatabase = mContext.openOrCreateDatabase("databaseName", mContext.MODE_PRIVATE, null);
        //  String findEditTableName = mSqLiteDatabase.findEditTable("users");
        try {
            //  创建表
            //  mSqLiteDatabase.execSQL(sqlite);

//            参数第(上下文，表名,null,数据库版本号)
            DBOpenHelper dbOpenHelper = new DBOpenHelper(mContext, TABLE_DATABASE_NAME, null, 2);
            mSqLiteDatabase = dbOpenHelper.getWritableDatabase();
        } catch (Exception e) {
            Log.d("table", "创建过了");
        }
    }

    /**
     * 关闭数据库
     */
    public void closeCreateDatabase() {
        mSqLiteDatabase.close();
        mSqLiteDatabase = null;
    }

    /**
     * 插入
     *
     * @param users 对象
     * @return 失败
     */
    public long insertData(Users users) {
        if (mSqLiteDatabase != null && users != null) {
            ContentValues values = new ContentValues();
            values.put("names", users.getNames());
            values.put("pwd", users.getPwd());
            return mSqLiteDatabase.insert("users", "", values);
        }
        return Long.valueOf("失败");
    }


    public int deleteData(int id) {
        String sqldelete = "_id=?";
        if (mSqLiteDatabase != null && id > 0) {
            return mSqLiteDatabase.delete("users", sqldelete, new String[]{String.valueOf(id)});
        }
        return -1;

    }

    public int updateData(Users users, int id) {
        if (users != null && mSqLiteDatabase != null) {
            ContentValues values = new ContentValues();
            values.put("names", users.getNames());
            values.put("pwd", users.getPwd());
            return mSqLiteDatabase.update("users", values, "_id=?", new String[]{String.valueOf(id)});

        }
            return -1;

    }

    //查询所有数据
    public Cursor selectAllData() {
        if (mSqLiteDatabase != null) {
            return mSqLiteDatabase.query("users", new String[]{"_id", "names", "pwd"}, null, null, null, null, null);
        }
        return null;

    }

    public Cursor selectData(String selection,
                             String[] selectionArgs, String groupBy, String having,
                             String orderBy) {
        if (mSqLiteDatabase != null) {
            return mSqLiteDatabase.query("users", new String[]{"names", "pwd"}, selection, selectionArgs, groupBy, having, orderBy);
        }
        return null;

    }

}
